<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="JavaScript判断数据的类型的方法12345678910111213141516171819202122232425262728let obj &#x3D; &amp;#123;  a: 1,  h: new Number(3),  b: &amp;#x27;a&amp;#x27;,  i: new String(&amp;#x27;12&amp;#x27;),  q: &#96;sdcard&#96;,  c: true,  j: new Boolean">
<meta property="og:type" content="article">
<meta property="og:title" content="数据类型判断">
<meta property="og:url" content="http://tallgy.gitee.io/2021/10/17/JavaScript/%E9%9A%8F%E7%AC%94/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="JavaScript判断数据的类型的方法12345678910111213141516171819202122232425262728let obj &#x3D; &amp;#123;  a: 1,  h: new Number(3),  b: &amp;#x27;a&amp;#x27;,  i: new String(&amp;#x27;12&amp;#x27;),  q: &#96;sdcard&#96;,  c: true,  j: new Boolean">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-17T07:08:23.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.354Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="数据类型判断">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2021/10/17/JavaScript/%E9%9A%8F%E7%AC%94/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/10/17/JavaScript/%E9%9A%8F%E7%AC%94/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD/","path":"2021/10/17/JavaScript/随笔/数据类型判断/","title":"数据类型判断"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>数据类型判断 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript%E5%88%A4%E6%96%AD%E6%95%B0%E6%8D%AE%E7%9A%84%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">JavaScript判断数据的类型的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#typeof"><span class="nav-number">1.1.</span> <span class="nav-text">typeof</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#instanceof"><span class="nav-number">1.2.</span> <span class="nav-text">instanceof</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#constructor-NaN"><span class="nav-number">1.3.</span> <span class="nav-text">constructor</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Object-prototype-toString-call"><span class="nav-number">1.4.</span> <span class="nav-text">Object.prototype.toString.call()</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/10/17/JavaScript/%E9%9A%8F%E7%AC%94/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据类型判断
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-17 15:08:23" itemprop="dateCreated datePublished" datetime="2021-10-17T15:08:23+08:00">2021-10-17</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="JavaScript判断数据的类型的方法"><a href="#JavaScript判断数据的类型的方法" class="headerlink" title="JavaScript判断数据的类型的方法"></a>JavaScript判断数据的类型的方法</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">let obj = &#123;</span><br><span class="line">  a: 1,</span><br><span class="line">  h: new Number(3),</span><br><span class="line"></span><br><span class="line">  b: &#x27;a&#x27;,</span><br><span class="line">  i: new String(&#x27;12&#x27;),</span><br><span class="line">  q: `sdcard`,</span><br><span class="line"></span><br><span class="line">  c: true,</span><br><span class="line">  j: new Boolean(false),</span><br><span class="line"></span><br><span class="line">  f: [1, 2, 3],</span><br><span class="line">  g: [&#x27;1&#x27;, &#x27;3&#x27;],</span><br><span class="line"></span><br><span class="line">  k: new Object(&#123; a: 3&#125; ),</span><br><span class="line">  l: &#123; b: 4 &#125;,</span><br><span class="line"></span><br><span class="line">  o: new RegExp(&#x27;sss&#x27;),</span><br><span class="line">  p: /sss/,</span><br><span class="line"></span><br><span class="line">  d: undefined,</span><br><span class="line">  e: null,</span><br><span class="line">  n: new Date(),</span><br><span class="line">  m: function () &#123;&#125;,</span><br><span class="line">  r: new Set([2, &#x27;1&#x27;, &#x27;aaa&#x27;]),</span><br><span class="line">  s: new Map([[&#x27;a&#x27;, 1], [2, &#x27;aa&#x27;], [&#x27;xxx&#x27;, 123]]),</span><br><span class="line">  t: Symbol(1),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="typeof"><a href="#typeof" class="headerlink" title="typeof"></a>typeof</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for (const k in obj) &#123;</span><br><span class="line">  console.log(&#x27; --  &#x27;);</span><br><span class="line">  console.log(obj[k]);</span><br><span class="line">  console.log(typeof obj[k]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"> --</span><br><span class="line">1</span><br><span class="line">number</span><br><span class="line"> --</span><br><span class="line">[Number: 3]</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">a</span><br><span class="line">string</span><br><span class="line"> --</span><br><span class="line">[String: &#x27;12&#x27;]</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">sdcard</span><br><span class="line">string</span><br><span class="line"> --</span><br><span class="line">true</span><br><span class="line">boolean</span><br><span class="line"> --</span><br><span class="line">[Boolean: false]</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">[ 1, 2, 3 ]</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">[ &#x27;1&#x27;, &#x27;3&#x27; ]</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">&#123; a: 3 &#125;</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">&#123; b: 4 &#125;</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">/sss/</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">/sss/</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">undefined</span><br><span class="line">undefined</span><br><span class="line"> --</span><br><span class="line">null</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">2021-10-20T01:55:29.835Z</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">[Function: m]</span><br><span class="line">function</span><br><span class="line"> --</span><br><span class="line">Set(3) &#123; 2, &#x27;1&#x27;, &#x27;aaa&#x27; &#125;</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">Map(3) &#123; &#x27;a&#x27; =&gt; 1, 2 =&gt; &#x27;aa&#x27;, &#x27;xxx&#x27; =&gt; 123 &#125;</span><br><span class="line">object</span><br><span class="line"> --</span><br><span class="line">Symbol(1)</span><br><span class="line">symbol</span><br></pre></td></tr></table></figure>

<p>这里的话，主要问题是在于</p>
<p>​    一个是,只能把数据分为基本数据类型和对象,并且使用new方法调用的就为对象.</p>
<p>​    一个是 null  的 typeof 的值为 object，至于原因的话</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://2ality.com/2013/10/typeof-null.html</span><br></pre></td></tr></table></figure>

<p>通过这篇文章我们可以知道原理，我这里就简单的说一下</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">首先，早期的JavaScript是通过值的低1位或3位来辨别类型的。</span><br><span class="line">又下面所示</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">000：对象。数据是对对象的引用。</span><br><span class="line">1：整数。数据是一个 31 位有符号整数。</span><br><span class="line">010：双倍。数据是对双浮点数的引用。</span><br><span class="line">100：字符串。数据是对字符串的引用。</span><br><span class="line">110：布尔值。数据是一个布尔值。</span><br><span class="line"></span><br><span class="line">undefined，用整数−2^30（负2的30次方，不在整型的范围内）</span><br><span class="line">null，机器码空指针（C/C++ 宏定义），低三位也是000</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">所以就出现了这种 typeof null = &#x27;object&#x27;;</span><br></pre></td></tr></table></figure>



<h2 id="instanceof"><a href="#instanceof" class="headerlink" title="instanceof"></a>instanceof</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/instanceof</span><br></pre></td></tr></table></figure>

<p><code>instanceof</code> 运算符用于检测构造函数的 <code>prototype</code> 属性是否出现在某个实例对象的原型链上。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.log(&#x27;a ----     &#x27; + a instanceof Number);</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">对于基本数据类型会直接返回false。而不会再内部构建对应的引用类型.</span><br><span class="line"></span><br><span class="line">否则的话，就会先通过 Object.getPrototypeOf 获得参数的原型对象。然后判断这个和你要判断的对象是否相等，不想等则会继续向上，直到相等或者为 null 时。</span><br></pre></td></tr></table></figure>



<h2 id="constructor-NaN"><a href="#constructor-NaN" class="headerlink" title="constructor"></a>constructor</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes/constructor</span><br></pre></td></tr></table></figure>

<p>**<code>constructor </code>**是一种用于创建和初始化<code>class</code>创建的对象的特殊方法。</p>
<p>你可以这样理解，一个对象的constructor的指向的是他所 new 而指向的方法。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function O() &#123;&#125;</span><br><span class="line">let o = new O();</span><br><span class="line"></span><br><span class="line">o.constructor === O; //true</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">console.log(&#x27;a ----     &#x27; + (a.constructor === Number));</span><br><span class="line">true</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">这里，在a调用constructor的时候，会在内部创建一个值为a的对象，然后再调用这个constructor方法，然后再将这个对象删除，所以这个是基本数据类型也可以进行判断的。</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">这个输出的值是 a的构造器所指向的一个函数。在这里就是指向的Number，</span><br><span class="line">问题：这个因为是通过判断其构造器函数来进行的判断，所以如果我们将对其进行过修改则会判断错误。</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function Fn() &#123;&#125;</span><br><span class="line">let x = new Fn();</span><br><span class="line">Fn.prototype = new Array();</span><br><span class="line">console.log(x.constructor === Fn)		//true</span><br><span class="line">console.log(x.constructor === Array)	//false</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function Fn() &#123;&#125;</span><br><span class="line">Fn.prototype = new Array();</span><br><span class="line">let x = new Fn();</span><br><span class="line">console.log(x.constructor === Fn)		//false</span><br><span class="line">console.log(x.constructor === Array)	//ture</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">第一个是先new，然后在修改的原型。</span><br><span class="line">而第二个是先修改原型，然后再进行new方法的调用。</span><br><span class="line">然后结果就不一样了，所以说明了这个new方法会调用的是原型。</span><br><span class="line">所以其构造器会被修改。</span><br></pre></td></tr></table></figure>

<p>通过上面这个代码我们可以知道，</p>
<p>你可以这样想,一个new方法和其结果是平级的</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">let a = new Fn();</span><br><span class="line">a,和fn是平级的.</span><br></pre></td></tr></table></figure>

<p>一个new方法，会将他的原型作为一个构造器,并将他自己进行一次<code>this</code>指向的调用.并创建一个 <code>__proto__</code> 指向了构造器的原型.</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">function Fn() &#123;&#125;</span><br><span class="line">let x = new Fn();</span><br><span class="line">console.log(x.constructor === Fn)		//true</span><br><span class="line">console.log(x.__proto__ === Fn.prototype)	//true</span><br></pre></td></tr></table></figure>



<h2 id="Object-prototype-toString-call"><a href="#Object-prototype-toString-call" class="headerlink" title="Object.prototype.toString.call()"></a>Object.prototype.toString.call()</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/toString</span><br></pre></td></tr></table></figure>

<p>使用方法</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Object.prototype.toString.call(a);</span><br><span class="line">这里通过调用了call方法，转换了this的指向。</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">let obj = &#123;</span><br><span class="line">  a: 1,</span><br><span class="line">  h: new Number(3),</span><br><span class="line"></span><br><span class="line">  b: &#x27;a&#x27;,</span><br><span class="line">  i: new String(&#x27;12&#x27;),</span><br><span class="line">  q: `sdcard`,</span><br><span class="line"></span><br><span class="line">  c: true,</span><br><span class="line">  j: new Boolean(false),</span><br><span class="line"></span><br><span class="line">  f: [1, 2, 3],</span><br><span class="line">  g: [&#x27;1&#x27;, &#x27;3&#x27;],</span><br><span class="line"></span><br><span class="line">  k: new Object(&#123; a: 3&#125; ),</span><br><span class="line">  l: &#123; b: 4 &#125;,</span><br><span class="line"></span><br><span class="line">  o: new RegExp(&#x27;sss&#x27;),</span><br><span class="line">  p: /sss/,</span><br><span class="line"></span><br><span class="line">  d: undefined,</span><br><span class="line">  e: null,</span><br><span class="line">  n: new Date(),</span><br><span class="line">  m: function () &#123;&#125;,</span><br><span class="line">  r: new Set([2, &#x27;1&#x27;, &#x27;aaa&#x27;]),</span><br><span class="line">  s: new Map([[&#x27;a&#x27;, 1], [2, &#x27;aa&#x27;], [&#x27;xxx&#x27;, 123]]),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">for (const k in obj) &#123;</span><br><span class="line">  console.log(&#x27; --  &#x27;);</span><br><span class="line">  console.log(obj[k]);</span><br><span class="line">  console.log(Object.prototype.toString.call(obj[k]));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过下面的结果我们可以看到,使用这个对象原型的方法可以说是没有任何问题啊.</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"> --</span><br><span class="line">1</span><br><span class="line">[object Number]</span><br><span class="line"> --</span><br><span class="line">[Number: 3]</span><br><span class="line">[object Number]</span><br><span class="line"> --</span><br><span class="line">a</span><br><span class="line">[object String]</span><br><span class="line"> --</span><br><span class="line">[String: &#x27;12&#x27;]</span><br><span class="line">[object String]</span><br><span class="line"> --</span><br><span class="line">sdcard</span><br><span class="line">[object String]</span><br><span class="line"> --</span><br><span class="line">true</span><br><span class="line">[object Boolean]</span><br><span class="line"> --</span><br><span class="line">[Boolean: false]</span><br><span class="line">[object Boolean]</span><br><span class="line"> --</span><br><span class="line">[ 1, 2, 3 ]</span><br><span class="line">[object Array]</span><br><span class="line"> --</span><br><span class="line">[ &#x27;1&#x27;, &#x27;3&#x27; ]</span><br><span class="line">[object Array]</span><br><span class="line"> --</span><br><span class="line">&#123; a: 3 &#125;</span><br><span class="line">[object Object]</span><br><span class="line"> --</span><br><span class="line">&#123; b: 4 &#125;</span><br><span class="line">[object Object]</span><br><span class="line"> --</span><br><span class="line">/sss/</span><br><span class="line">[object RegExp]</span><br><span class="line"> --</span><br><span class="line">/sss/</span><br><span class="line">[object RegExp]</span><br><span class="line"> --</span><br><span class="line">undefined</span><br><span class="line">[object Undefined]</span><br><span class="line"> --</span><br><span class="line">null</span><br><span class="line">[object Null]</span><br><span class="line"> --</span><br><span class="line">2021-10-20T01:55:45.116Z</span><br><span class="line">[object Date]</span><br><span class="line"> --</span><br><span class="line">[Function: m]</span><br><span class="line">[object Function]</span><br><span class="line"> --</span><br><span class="line">Set(3) &#123; 2, &#x27;1&#x27;, &#x27;aaa&#x27; &#125;</span><br><span class="line">[object Set]</span><br><span class="line"> --</span><br><span class="line">Map(3) &#123; &#x27;a&#x27; =&gt; 1, 2 =&gt; &#x27;aa&#x27;, &#x27;xxx&#x27; =&gt; 123 &#125;</span><br><span class="line">[object Map]</span><br><span class="line"> --</span><br><span class="line">Symbol(1)</span><br><span class="line">[object Symbol]</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript/" rel="tag"># JavaScript</a>
              <a href="/tags/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%88%A4%E6%96%AD/" rel="tag"># 数据类型判断</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/17/CSS/%E6%B0%B4%E5%B9%B3%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD%E6%96%B9%E6%B3%95/" rel="prev" title="水平垂直居中方法">
                  <i class="fa fa-chevron-left"></i> 水平垂直居中方法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/18/JavaScript/%E6%96%B9%E6%B3%95/call-apply-bind%E5%AE%9E%E7%8E%B0/" rel="next" title="call,apply,bind实现">
                  call,apply,bind实现 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
